const db = require('../config/db')

/**
 * 课程查询的接口逻辑
 */

exports.listVideo= (req,res)=>{
    //获取前端参数
    let {category,page,size} = req.query;
    page=(page-1)*size;

    //查询课程列表sql
    const pageSql = `select * from video where del=0 and category = ? order by id limit ?,?`;

    //查询课程总数sql
    const totalSql = `select count(*) as total from video where del=0 and category = ?`;


    db.query(pageSql,[category,Number(page),Number(size)],(err,resPage)=>{
        if(err){
            return res.send({
                code:1,
                message:err.message
            });
        }
        db.query(totalSql,[category],(err,resTotal)=>{
            if(err){
                return res.send({
                    code:1,
                    message:err.message
                });
            }
            res.send({
                code:0,
                data:{
                    list:resPage,
                    total:resTotal,
                },
            });
        });




});
};




/**
 * 课程修改接口逻辑
 */

exports.updateVideoById = (req,res)=>{
    let {title,price,id}=req.query;
    let sql = `update video set `;
    let arr = [];
    //同时修改标题和价格
    if(title&&price){
        sql = sql + 'title=?,price=? where id=?';
        arr = [title,Number(price),Number(id)];
    }else if(title){
        //只修改标题
        sql = sql + 'title=? where id=?';
        arr = [title,Number(id)];
    }else if(price){
        //只修改价格
        sql = sql + 'price=? where id=?';
        arr = [Number(price),Number(id)];
    }

    db.query(sql, arr, (err, result) => {
        if (err) {
            return res.send({ code: 1, message: err.message });
        }
        res.send({ code: 0, message: '修改成功' });
    });
};


/**
 * 课程删除接口逻辑
 */

exports.deleteVideoById = (req, res) => {
    let { id } = req.query;
    let deleteSql = 'update video set del=1 where id=?';
    db.query(deleteSql, id, (err, results) => {
      if (err) {
        return res.send({ code: 1, message: err.message });
      }
      res.send({
        code: 0,
        data: {
          message: '删除成功',
        },
      });
    });
  };